<?php
class CodeGenerator
{
	private $_appName;
	private $_moduleName;
	private $_actionName;

	public function main()
	{
		if (1 == count($_SERVER['argv']))
		{
			echo "Usage:
php codegen.php appName [, ModeuleName] [, ActionName]
			
Go to your project directory, and then execute:
php codegen.php frontend Hello HelloWorld
It equals to:
(1) php codegen.php frontend && php codegen.php frontend Hello HelloWorld
(2) php codegen.php frontend && php codegen.php frontend Hello && php codegen.php frontend Hello HelloWorld
			
*Suggestion: First character of ModuleName and ActionName are uppercase (lowercase can work can work too).
";
		}
		else
		{
			$this->_appName = $_SERVER['argv'][1];
			$this->_moduleName = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : "Index";
			$this->_actionName = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : "Index";
			$this->initProject();
			$this->initApp();
			$this->initModule();
			$this->initAction();
		}
	}

	public function initProject()
	{
		if(!is_dir('app'))
		{
			mkdir('app');
			mkdir('cli_entrance');
			mkdir('conf/standard',0777,true);
			mkdir('conf/dev',0777,true);
			mkdir('dist');
			mkdir('lib/3rd_party',0777,true);
			mkdir('lib/dao',0777,true);
			mkdir('lib/service',0777,true);
			mkdir('lib/util',0777,true);
			mkdir('web_entrance',0777,true);

			file_put_contents('conf/shared_conf.php', "<?php
\$myProjConfig = array();
foreach(glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'standard' . DIRECTORY_SEPARATOR . '*.php') as \$confFile)
{
	if (__FILE__ != \$confFile)
	{
		include(\$confFile);
	}
}");

			file_put_contents('conf/conf.php', "<?php
include('shared_conf.php');
return \$myProjConfig;");

			file_put_contents('conf/conf_dev.php', "<?php
include('shared_conf.php');
foreach(glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . '*.php') as \$devConfFile)
{
	if (__FILE__ != \$devConfFile)
	{
		include(\$devConfFile);
	}
}
return \$myProjConfig;");

			file_put_contents('conf/standard/db_server.php', "<?php
\$myProjConfig['db_server'] = array(
	'global' => array(
		'the_only_node' => array(
			'master' => array(
				'the_only_host' => array(
					'adapter' => 'pdoMysql',
					'username' => '',
					'password' => '',
					'dbname' => '',
)))));");

			file_put_contents('conf/standard/db_table.php', "<?php
\$myProjConfig['db_table'] = array(
	'some_table' => array(
		'group' => 'global',
		'schema' => 'global',
		'table_name' => ''
	),
);");
		}
	}

	public function initApp()
	{
		$appDir = "app/$this->_appName";
		if (isset($this->_appName) && !is_dir($appDir))
		{
			$appAbstractAction = ucfirst($this->_appName) . "Action";
			$appAbstractView = ucfirst($this->_appName) . "View";
			mkdir("$appDir/conf/standard",0777,true);
			mkdir("$appDir/conf/dev",0777,true);
			mkdir("$appDir/layout",0777,true);
			mkdir("$appDir/lib",0777,true);
			mkdir("$appDir/module",0777,true);
			mkdir("web_entrance/$this->_appName",0777,true);

			$kiwiRuntimeHome = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR;

			file_put_contents("web_entrance/$this->_appName/index.php", "<?php
include('{$kiwiRuntimeHome}kiwi.php');
Kiwi::setEnvMode('production');
Kiwi::run(array(
	'app_name' => '$this->_appName'
),'Index','Index');");

			file_put_contents("web_entrance/$this->_appName/{$this->_appName}_dev.php", "<?php
include('{$kiwiRuntimeHome}kiwi.php');
Kiwi::run(array(
	'app_name' => '$this->_appName',
	'config_file' => dirname(dirname(dirname(__FILE__))) . '/conf/conf_dev.php',
	'app_config_file' => dirname(dirname(dirname(__FILE__))) . '/app/$this->_appName/conf/conf_dev.php',
),'Index','Index');");

			file_put_contents("app/$this->_appName/lib/$appAbstractAction.php", "<?php
class $appAbstractAction extends Action
{
}");

			file_put_contents("app/$this->_appName/layout/frontpage.php", "<html>
<head>
	<title><?php echo \$this->context->uri['action'] ?></title>
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=<?php echo Config::\$app['misc']['encoding'] ?>\">
	<base href=\"<?php echo \$this->context->uri['base']; ?>\" />
</head>
<body>
page head
<hr />
<?php
include(\$this->context->view['template_dir'] . \$this->context->view['template'] . '.php');
?>
<hr />
page tail
</body>
</html>");

			file_put_contents("app/$this->_appName/conf/conf.php", "<?php
include('shared_conf.php');
return \$myAppConfig;");

			file_put_contents("app/$this->_appName/conf/conf_dev.php", "<?php
include('shared_conf.php');
foreach(glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . '*.php') as \$devConfFile)
{
	if (__FILE__ != \$devConfFile)
	{
		include(\$devConfFile);
	}
}
return \$myAppConfig;");

			file_put_contents("app/$this->_appName/conf/shared_conf.php", "<?php
\$myAppConfig = array();
foreach(glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'standard' . DIRECTORY_SEPARATOR . '*.php') as \$confFile)
{
	if (__FILE__ != \$confFile)
	{
		include(\$confFile);
	}
}");
		}
	}

	public function initModule()
	{
		$moduleDir = "app/$this->_appName/module/$this->_moduleName";
		if (isset($this->_moduleName) && !is_dir($moduleDir))
		{
			mkdir("$moduleDir/action", 0777, true);
			mkdir("$moduleDir/view", 0777, true);
		}
	}

	public function initAction()
	{
		$actionClass = $this->_actionName . 'Action';
		$actionFile = "app/$this->_appName/module/$this->_moduleName/action/$actionClass.php";
		if (isset($this->_actionName) && !file_exists($actionFile))
		{
			$parentAction = ucfirst($this->_appName) . 'Action';
				
			file_put_contents($actionFile, "<?php
class $actionClass extends $parentAction
{
	public function execute()
	{
		\$this->view->render();
	}
}");
			file_put_contents("app/$this->_appName/module/$this->_moduleName/view/$this->_actionName.php", "<pre>
app: $this->_appName
module: $this->_moduleName
action: $this->_actionName
</pre>");
		}
	}
}
$codeGenerator = new CodeGenerator();
$codeGenerator->main();
